package net.imwork.amorgemini.core.security;

import net.imwork.amorgemini.entity.SysUser;
import net.imwork.amorgemini.repository.mysql.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * Created by lvbr on 2017-07-29 19:38.
 */
@Service
public class CustomUserDetailsService implements UserDetailsService {

    private static final Logger logger = LoggerFactory.getLogger(CustomUserDetailsService.class);
    @Autowired
    UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userRepository.getByUname(username);
//        user.getSysUserRolesById().;
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        //用于添加用户的权限。只要把用户权限添加到authorities 就万事大吉。
        /*List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (SysRole role : user.getRoles()) {
            authorities.add(new SimpleGrantedAuthority(role.getSysRoleByRoleId().getName()));
            logger.info("获取用户={}角色Role={}", username, role.getSysRoleByRoleId().getName());
        }*/

        return null;
//        return new User(user.getUsername(), user.getPassword(), authorities);
    }
}
